package com.april.service.impl;

import com.april.mapper.TbItemCatMapper;
import com.april.mapper.TbItemDescMapper;
import com.april.mapper.TbItemMapper;
import com.april.mapper.TbItemParamItemMapper;
import com.april.pojo.*;
import com.april.service.ItemService;
import com.april.utils.IDUtils;
import com.april.utils.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 槐序 廿一
 * @Date: 2023-06-19
 * @Time: 17:42
 * @Description:
 */
@Service
@Transactional
public class ItemServiceImpl implements ItemService {

    @Resource
    TbItemMapper tbItemMapper;

    @Resource
    TbItemDescMapper tbItemDescMapper;

    @Resource
    TbItemParamItemMapper tbItemParamItemMapper;

    @Resource
    TbItemCatMapper tbItemCatMapper;

    @Override
    public PageResult findItemByPage(int page, int rows) {
        // 开启分页
        PageHelper.startPage(page, rows);
        // 查询SQL
        TbItemExample tbItemExample = new TbItemExample();
        tbItemExample.setOrderByClause("updated DESC");
        tbItemExample.createCriteria().andStatusEqualTo((byte) 1);
        List<TbItem> tbItemList = tbItemMapper.selectByExample(tbItemExample);
        for (TbItem tbItem : tbItemList) {
            tbItem.setPrice(tbItem.getPrice() / 100);
            tbItemMapper.updateByPrimaryKeySelective(tbItem);
        }
        PageInfo<TbItem> pageInfo = new PageInfo<>(tbItemList);
        PageResult pageResult = new PageResult();
        pageResult.setPageIndex(pageInfo.getPageNum());//设置页码
        pageResult.setTotalPage(Long.valueOf(pageInfo.getPages()));//设置总页数
        pageResult.setResult(pageInfo.getList());//设置当前页码数据
        return pageResult;
    }

    @Override
    public int insertTbItem(TbItem tbItem, String desc, String itemParams) {
        long id = IDUtils.genItemId();
        tbItem.setId(id);
        tbItem.setPrice(tbItem.getPrice() * 100);
        tbItem.setStatus((byte) 1);
        tbItem.setCreated(new Date());
        tbItem.setUpdated(new Date());
        int firstInsert = tbItemMapper.insert(tbItem);
        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setItemId(id);
        tbItemDesc.setItemDesc(desc);
        tbItemDesc.setCreated(new Date());
        tbItemDesc.setUpdated(new Date());
        int secondInsert = tbItemDescMapper.insert(tbItemDesc);
        TbItemParamItem tbItemParamItem = new TbItemParamItem();
        tbItemParamItem.setParamData(itemParams);
        tbItemParamItem.setItemId(id);
        tbItemParamItem.setCreated(new Date());
        tbItemParamItem.setUpdated(new Date());
        int thirdInsert = tbItemParamItemMapper.insert(tbItemParamItem);
        return firstInsert + secondInsert + thirdInsert;
    }

    @Override
    public Map<String, Object> preUpdateItem(Long itemId) {
        Map<String, Object> map = new HashMap<>();
        TbItem tbItem = tbItemMapper.selectByPrimaryKey(itemId);
        map.put("item", tbItem);
        TbItemDesc itemDesc = tbItemDescMapper.selectByPrimaryKey(itemId);
        map.put("itemDesc", itemDesc.getItemDesc());
        TbItemCat tbItemCat = tbItemCatMapper.selectByPrimaryKey(tbItem.getCid());
        map.put("itemCat", tbItemCat.getName());
        TbItemParamItemExample tbItemParamItemExample = new TbItemParamItemExample();
        tbItemParamItemExample.createCriteria().andItemIdEqualTo(itemId);
        TbItemParamItem tbItemParamItem = tbItemParamItemMapper.selectByExampleWithBLOBs(tbItemParamItemExample).get(0);
        map.put("itemParamItem", tbItemParamItem.getParamData());
        return map;
    }

    @Override
    public int updateTbItem(TbItem tbItem, String desc, String itemParams) {
        return 0;
    }
}
